let resultLoader = require("./niuniuResult").loadFromDocument;
let resultCreator = require("./niuniuResult").create;

function NiuniuGameResult() {
    this.gid = 0;
    this.tid = 0;
    this.code = "";
    this.multiplier = 0;
    this.startTime = 0;
    this.endTime = 0;
    this.dealerType = 0;
    this.multiplierType = "";
    this.maxAuctionDealerMultiplier = 0;
    this.results = [];
}

NiuniuGameResult.prototype.toBSON = function() {
    let serializeResults = [];
    for(let result of this.results) {
        serializeResults.push(result.toBSON());
    }
    return {
        gid : this.gid,
        tid: this.tid,
        code : this.code,
        multiplier : this.multiplier,
        startTime: this.startTime,
        endTime : this.endTime,
        dealerType : this.dealerType,
        multiplierType: this.multiplierType,
        maxAuctionDealerMultiplier : this.maxAuctionDealerMultiplier,
        results : serializeResults
    };
};

NiuniuGameResult.prototype.toJSON = function () {
    let serializeResults = [];
    for(let result of this.results) {
        serializeResults.push(result.toJSON());
    }
    return {
        gid : this.gid,
        tid: this.tid,
        code : this.code,
        multiplier : this.multiplier,
        startTime: this.startTime,
        endTime : this.endTime,
        dealerType : this.dealerType,
        multiplierType: this.multiplierType,
        maxAuctionDealerMultiplier : this.maxAuctionDealerMultiplier,
        results : serializeResults
    };
};

module.exports.create = function(gid, tid, code, multiplier, startTime, endTime, dealerType,
                                 multiplierType, maxAuctionDealerMultiplier, results) {
    let niuniuGameResult = new NiuniuGameResult();
    niuniuGameResult.gid = gid;
    niuniuGameResult.tid = tid;
    niuniuGameResult.code = code;
    niuniuGameResult.multiplier = multiplier;
    niuniuGameResult.startTime = startTime;
    niuniuGameResult.endTime = endTime;
    niuniuGameResult.dealerType = dealerType;
    niuniuGameResult.multiplierType = multiplierType;
    niuniuGameResult.maxAuctionDealerMultiplier = maxAuctionDealerMultiplier;
    niuniuGameResult.results = [];
    for(let result of results) {
        niuniuGameResult.results.push(resultCreator(result.uid, result.score, result.niuniuType, result.cards));
    }
    return niuniuGameResult;
};

module.exports.loadFromDocument = function (document) {
    let gameResult = new NiuniuGameResult();
    gameResult.gid = document["gid"];
    gameResult.tid = document["tid"];
    gameResult.code = document["code"];
    gameResult.multiplier = document["multiplier"];
    gameResult.startTime = document["startTime"];
    gameResult.endTime = document["endTime"];
    gameResult.dealerType = document["dealerType"];
    gameResult.multiplierType = document["multiplierType"];
    gameResult.maxAuctionDealerMultiplier = document["maxAuctionDealerMultiplier"];
    gameResult.results = [];
    for(let result of document.results) {
        gameResult.results.push(resultLoader(result));
    }
    return gameResult;
};
